#include "HttpUpload.h"
#include "ui_HttpUpload.h"
#include <QFileDialog>
#include <QFile>
#include <QDebug>
#include <QHttpMultiPart>
#include <QHttpPart>

HttpUpload::HttpUpload(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::HttpUpload)
{
    ui->setupUi(this);


    ui->progressBar->setMinimum(0);
    ui->progressBar->setMaximum(100);
}

HttpUpload::~HttpUpload()
{
    delete ui;
}


void HttpUpload::on_pushButton_selectUploadFile_clicked()
{
    QFileDialog *fileDialog = new QFileDialog(this);
    //定义文件对话框标题
    fileDialog->setWindowTitle(QStringLiteral("选中文件"));
    //设置默认文件路径
    fileDialog->setDirectory(".");
    //设置文件过滤器
    fileDialog->setNameFilter(tr("File(*.*)"));
    //设置可以选择多个文件,默认为只能选择一个文件QFileDialog::ExistingFiles
    fileDialog->setFileMode(QFileDialog::ExistingFiles);
    //设置视图模式
    fileDialog->setViewMode(QFileDialog::Detail);
    //打印所有选择的文件的路径
    QStringList fileNames;
    if (fileDialog->exec()) {
        fileNames = fileDialog->selectedFiles();
    }

    //如果选择的文件不为空则显示到界面上
    if(!fileNames.isEmpty())
    {
        ui->lineEdit->setText(fileNames.first());
    }
}

void HttpUpload::on_pushButton_startUpload_clicked()
{
    if(_pUploadManager == nullptr)
    {
        _pUploadManager = new QNetworkAccessManager(this);
    }
 #ifdef Byte


    QString uploadFileName = ui->lineEdit->text();
    QFile file(uploadFileName);
    if(!file.open(QIODevice::ReadOnly))
    {
        qWarning("file open fail");

        return;
    }

    QByteArray byteFileData = file.readAll();

    file.close();
    QString  uploadUrl = ui->lineEdit_UploadURL->text();
    QUrl url(uploadUrl+"?filename="+file.fileName());

    QNetworkRequest request(url);
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/octet-stream");

     _pReply = _pUploadManager->post(request, byteFileData);
#else
    QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);


    QString uploadFileName = ui->lineEdit->text();

    QFileInfo fileInfo(uploadFileName);

    QString fileName = fileInfo.fileName();


    QHttpPart imagePart;
    imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/png"));
    imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QString("form-data; name=\"uploadfile\";filename=\"%1\"").arg(fileName)));
    QFile *file = new QFile(uploadFileName);
    file->open(QIODevice::ReadOnly);
    imagePart.setBodyDevice(file);
    file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart

    multiPart->append(imagePart);

    QString urlServerStr = ui->lineEdit_UploadURL->text();
    QUrl url(urlServerStr);
    QNetworkRequest request(url);


    _pReply = _pUploadManager->post(request, multiPart);
    multiPart->setParent(_pReply); // delete the multiPart with the reply
#endif

    connect(_pReply, &QNetworkReply::finished,this, &HttpUpload::onUpLoadFinished);
    connect(_pReply, &QNetworkReply::uploadProgress, this, &HttpUpload::OnUploadProgress);
}

void HttpUpload::onUpLoadFinished()
{
    qInfo()<<"上传状态"<<QString(_pReply->readAll());

    delete _pReply;
    _pReply = nullptr;
}

void HttpUpload::onUpLoadError(QNetworkReply::NetworkError errorCode)
{
 qInfo()<<"上传出错"<<errorCode;
}

void HttpUpload::OnUploadProgress(qint64 bytesSent, qint64 bytesTotal)
{
    if(bytesTotal <= 0)
    {
        return;
    }
    ui->progressBar->setValue(bytesSent/bytesTotal*100);
}
